package com.bangdream.michelia.view.activity.my;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.application.AppCurrentUser;
import com.bangdream.michelia.contract.CollectionContract;
import com.bangdream.michelia.entity.CollectionBean;
import com.bangdream.michelia.entity.CollectionRecordBean;
import com.bangdream.michelia.entity.UserBean;
import com.bangdream.michelia.entity.courses.CoursesBean;
import com.bangdream.michelia.entity.courses.TeacherListBean;
import com.bangdream.michelia.presenter.CollectionPresenter;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.utils.PublicUtil;
import com.bangdream.michelia.view.activity.base.SwipeBackActivity;
import com.bangdream.michelia.view.activity.currency.MainActivity;
import com.bangdream.michelia.view.activity.curriculum.CurriculumDetails;
import com.bangdream.michelia.view.adapter.CollectionAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.List;

import static android.R.attr.data;
import static android.R.attr.fastScrollOverlayPosition;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

/**
 * @date on 2018/7/24 2:05
 * @email 909732184@qq.com
 * 我的收藏
 */
public class MyCollection extends SwipeBackActivity<CollectionContract.ICollectionView,CollectionPresenter<CollectionContract.ICollectionView>>
implements CollectionContract.ICollectionView{
    private XRefreshView refreshView;
    private SwipeMenuRecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private CollectionAdapter adapter;
    private int page = 1;
    private UserBean userBean;
    private int curPosition;
//    private List
    @Override
    public int getLayout() {
        return R.layout.activity_my_collection;
    }

    @Override
    public void findId() {
        refreshView = findViewById(R.id.refreshView);
        refreshView.setPullLoadEnable(false);
        refreshView.setPullRefreshEnable(true);
        recyclerView = findViewById(R.id.recyclerView);
        initEmptyView(recyclerView);

        SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(com.yanzhenjie.recyclerview.swipe.SwipeMenu swipeLeftMenu, com.yanzhenjie.recyclerview.swipe.SwipeMenu swipeRightMenu, int viewType) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(MyCollection.this)
                        .setBackgroundColor(getResources().getColor(R.color.white)) // 背景颜色
                        .setText("删除") // 文字。
                        .setTextColor(Color.BLACK) // 文字颜色。
                        .setTextSize(16) // 文字大小。
                        .setWidth(144) // 宽
                        .setHeight(MATCH_PARENT); //高（MATCH_PARENT意为Item多高侧滑菜单多高 （推荐使用））
                swipeRightMenu.addMenuItem(deleteItem);// 添加一个按钮到右侧侧菜单。
            }
        };

        // 4. 创建侧滑菜单的点击事件
        SwipeMenuItemClickListener mMenuItemClickListener = new SwipeMenuItemClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge) {
                // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
                menuBridge.closeMenu();
                //在menuBridge中我们可以得到侧滑的这一项item的position (menuBridge.getAdapterPosition())
//                Toast.makeText(MyCollection.this,"位置"+menuBridge.getAdapterPosition(),Toast.LENGTH_SHORT).show();
                curPosition = menuBridge.getAdapterPosition();
                Log.i("curPosition",""+curPosition);
                mPresenter.actionCencelCollection(adapter.getData().get(curPosition).getId(),null);
                Log.i("curPosition",""+curPosition+adapter.getData().get(curPosition).getCourseId()+"\n"+adapter.getData().get(curPosition).getId());
//
            }
        };
        // 设置菜单创建器

        recyclerView.setSwipeMenuCreator(swipeMenuCreator);
        // 设置菜单Item点击监听
        recyclerView.setSwipeMenuItemClickListener(mMenuItemClickListener);

        WindowManager wm = this.getWindowManager();
        noDataView.getLayoutParams().height=wm.getDefaultDisplay().getHeight()- PublicUtil.dip2px(this,50);;

    }

    @Override
    public void setListener() {
        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                page++;
                getData();
            }
        });

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter a, View view, int position) {
                CollectionBean coursesBean = adapter.getData().get(position);
                if(coursesBean!=null) {
                    String CourseId = coursesBean.getCourseId();
                    Intent intent = new Intent(MyCollection.this, CurriculumDetails.class);
                    intent.putExtra("id",CourseId);
                    intent.putExtra("title",coursesBean.getCourse().getName());
//                    List<TeacherListBean> teacherList = coursesBean.getCourse().getTeacherList();
//                    if(teacherList!=null && teacherList.size()>0)
//                        intent.putExtra("teacherId",teacherList.get(0).getTeacherId());
                    startActivity(intent);
                }else {
                    showText("收藏课程中数据错误");
                }
            }
        });


    }


    private void getData(){
//        adapter.setEmptyView(loadingView);
        mPresenter.getCollectionList(page, PublicResources.PageSize,userBean.getId(),null);
    }
    @Override
    public void logic() {
        initTitleBar(" ","我的收藏","",this);
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        refreshView.setPullLoadEnable(true);
        refreshView.setPullRefreshEnable(true);
        adapter = new CollectionAdapter(R.layout.my_collection_adapter_item);
        recyclerView.setAdapter(adapter);
        userBean = AppCurrentUser.getInstance().getUserInfo();
        getData();
    }

    @Override
    public CollectionPresenter createPresenter() {
        return new CollectionPresenter();
    }

    @Override
    public void setCollectionList(boolean isSuccess, int page, List<CollectionBean> data, String stDescribe) {
        if (isSuccess&&data!=null){
            if(page==1) {
                adapter.setNewData(data);
                adapter.notifyDataSetChanged();
                refreshView.stopRefresh();
                //显示数据大于20条才允许上拉加载更多
                if(data.size()>=PublicResources.PageSize){
                    refreshView.setPullLoadEnable(true);
                }
                if(data.size()==0){
                    adapter.setEmptyView(noDataView);
                }
            }else {
                adapter.getData().clear();
                adapter.addData(data);
                refreshView.stopLoadMore();
            }
        }else {
            //如果是加载新数据时失败了 则page-1
            if (page>1){
                refreshView.stopLoadMore();
                this.page--;
            }else {
                //还是还原为1防止出现负数
                this.page = 1;
                refreshView.stopRefresh();
                if(data==null){
                    adapter.setEmptyView(noDataView);
                }else
                adapter.setEmptyView(errorView);
            }
        }
    }

    @Override
    public void actionCollection(boolean isSuccess, CollectionRecordBean data, String stDescribe) {

    }

    @Override
    public void actionCencelCollection(boolean isSuccess, String stDescribe) {
        if (isSuccess) {
            List<CollectionBean> tempData = adapter.getData();
            tempData.remove(curPosition);
            adapter.setNewData(tempData);
            adapter.notifyDataSetChanged();
            if(tempData.size()==0){
                adapter.setEmptyView(noDataView);
            }
        }

    }
}
